<?php
namespace app\api\job;

use app\api\controller\GameNotify;
use app\common\model\Order;
use app\common\model\OrderLog;
use think\facade\Log;
use think\queue\Job;

class GameNotifyJob {
    public function fire(Job $job, $data)
    {
        $c = new GameNotify();
        $result = $c->game_notify($data['order_no']);
        if($result['status']) {
            $job->delete();
        } else {
        // 通知失败
            if ($job->attempts() > 3) {
                // 超过重试次数
                $this->failed($data);
                $job->delete();
            } else {
                // 3秒后重试
                $job->release(3);
            }
        }
    }

    public function failed($data)
    {
        $order = Order::where('order_no', $data['order_no'])->find();
        $order->order_status = 4;
        $order->save();
        OrderLog::addLog($order->order_no, $order->order_status, $order->type, $order->trade_no, '通知失败');
        
        Log::error("订单：{$data['order_no']} 通知失败");
    }
}
